<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\exception\UploadException;
use app\common\library\Upload;
use app\common\model\Canal;
use app\common\model\Collision;
use app\common\model\Ibec;
use app\common\model\Type;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 上传
     *
     */
    public function upload()
    {
        $action = $this->request->param('action');
        switch($action){
            case 'config':
                $result = file_get_contents(ROOT_PATH.'/public/assets/addons/ueditor/config.json');
                break;
            case 'uploadimage':
                $attachment = null;
                //默认普通上传文件
                $file = $this->request->file('upfile');
                try {
                    $upload = new Upload($file);
                    $attachment = $upload->upload();
                } catch (UploadException $e) {
                    $this->error($e->getMessage());
                }
                if($file){
                    $res['state'] = 'SUCCESS';
                    $res['url'] = cdnurl($attachment->url, true);
                    $res['name'] = $attachment->filename;
                    $result = json_encode($res);
                }
                break;
            default:
                break;
        }

        return $result;
    }


    /**
     * 获取客户类型
     * */
    public function getType(){
        $list=Type::field("id,name")->order("weigh","desc")->select();
        $this->success("获取成功",$list);
    }
    /**
     * 获取配置
     * */
    public function getConfig(){
        $list["name"]=\think\Config::get('site.name');
        $list["address"]=\think\Config::get('site.address');
        $list["lat"]=\think\Config::get('site.lat');
        $list["lng"]=\think\Config::get('site.lng');
        $list["contactNumber"]=\think\Config::get('site.contactNumber');
        $list["lotNo"]=\think\Config::get('site.lotNo');
        $list["featuredLabel"]=\think\Config::get('site.featuredLabel');
        $list["content"]=\think\Config::get('site.content');
        $list["banner"]=\think\Config::get('site.banner');
        $list["appointCanal"]=\think\Config::get('site.appointCanal');
        $this->success("成功",$list);
    }

    /**
     * 获取经纪公司
     * */
    public function getIbec(){
        $list=Ibec::field("id,name")->whereLike("name","%".$this->request->param('name')."%")
            ->order("createtime","desc")->select();
        $this->success("获取成功",$list);
    }

    /**
     * 置业顾问列表
     * */
    public function canalList(){
        $list=Canal::field("id,name")->order("createtime","desc")->whereLike("name","%".$this->request->param('name')."%")->select();
        $this->success("获取成功",$list);
    }
    /**
     * 2023-6-25 修改绑定时间
     * */
    public function updateBind(){
        $model=new \app\common\model\Connection();
        $connection = $model->where("lapse_time > 0 and status <> '4'")->where("YEAR(FROM_UNIXTIME(lapse_time)) =2024")->select();//已绑定客户
        $updata=[];
        foreach ($connection as $k=>$v){
           $lapse_time = strtotime("+30 days", strtotime(date("Y-m-d", time())));
           $updata[$k]["lapse_time"] =$lapse_time;
           $updata[$k]["id"] = $v["id"];
        }
        if(!empty($updata)){
            $model->saveAll($updata);
        }
    }
    /**
     * 2023-6-27 清除未绑定客户置业顾问
     * */
    public function clearCanal(){
        $model=new \app\common\model\Connection();
        $connection = $model->whereNotIn("status",['2','3','4'])->where("canal_id >0")->select();
        if(!empty($connection)){
            $updata=[];
            foreach ($connection as $k=>$v){
                $updata[$k]["canal_id"] =0;
                $updata[$k]["id"] = $v["id"];
            }
            if(!empty($updata)){
                $model->saveAll($updata);
            }
        }
    }
}
